package com.csxy.lsh.controller;

import com.csxy.lsh.R;
import com.csxy.lsh.entity.EduChapter;
import com.csxy.lsh.entity.vo.ChapterVo;
import com.csxy.lsh.service.EduChapterService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "课程章节管理")
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {
    @Autowired
    private EduChapterService chapterService;

//获取课程大纲列表，并根据id进行查询
    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list= chapterService.getChaterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo",list);
    }


    //添加章节
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        boolean save=chapterService.save(eduChapter);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }
    }


    //根据章节id进行查询
    @GetMapping("/getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId){
        EduChapter eduChapter=chapterService.getById(chapterId);
        return R.ok().data("chapter",eduChapter);
    }

    //兼容旧前端路径
    @GetMapping("/getChapter/{chapterId}")
    public R getChapter(@PathVariable String chapterId){
        return getChapterInfo(chapterId);
    }

    //修改章节
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        chapterService.updateById(eduChapter);
        return R.ok();
    }
    //删除章节
    @DeleteMapping("/deleteById/{chapterId}")
    public R deleteById(@PathVariable String chapterId){
        boolean flag =chapterService.deleteChapter(chapterId);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
        }
}
